home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
58189
/
58189.xpi
/
components
/
CsFire.js
Wrap
Text File
|
2010-01-07
|
6KB
|
203 lines
/*
* This service is the main component of the CsFire extension. It defines
* all the required hooks to handle browser events. All the extension-specific
* code is contained elsewhere (probably CsFireController).
*/
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
// constants: shorter version
const CI = Components.interfaces;
const CC = Components.classes;
const CICP = CI.nsIContentPolicy;
/*
* Defines a new CsFire service, in a separate namespace, to avoid
* variable pollution.
*/
CsFireService = function() {};
CsFireService.prototype = {
classDescription: "Main component of the CsFire extension",
classID: Components.ID("{b6e72cf8-8cb7-4706-9582-985a09db5a2b}"),
contractID: "@cs.kuleuven.be/csfire;1",
QueryInterface : XPCOMUtils.generateQI([CICP, CI.nsIObserver]),
// The XPCOM events that require notification (separate registration process)
_xpcom_categories: [{
// After receiving this event, the component will initialize
category: "app-startup"
}, {
// This event is used to intercept requests from within the browser (not the HTTP requests)
category: "content-policy"
}],
/* Factory that creates a singleton instance of the component
[optional] custom factory (an object implementing nsIFactory). If not
provided, the default factory is used, which returns
|(new MyComponent()).QueryInterface(iid)| in its createInstance().
*/
_xpcom_factory: {
createInstance : function() {
if (CsFireService.instance == null) {
CsFireService.instance = new CsFireService();
}
return CsFireService.instance;
}
},
/*************************
* nsIObserver interface *
************************/
/*
* Handle notification messages
*/
observe : function(subject, topic, data) {
switch (topic) {
// HTTP events
case "http-on-examine-response" :
this.processHttpResponse(subject);
break;
case "http-on-modify-request" :
this.processHttpRequest(subject);
break;
// Application events
case "app-startup" :
this.init();
break;
case "xpcom-shutdown" :
this.shutdown();
break;
}
},
/*
* Handle extension startup
*/
init: function() {
this.loadModules(); // No logging before this point :)
CsFire.Logger.info("Starting CsFire");
//Register observer
var os = CC['@mozilla.org/observer-service;1'].getService(CI.nsIObserverService);
os.addObserver(this, "http-on-modify-request", false);
os.addObserver(this, "http-on-examine-response", false);
os.addObserver(this, "xpcom-shutdown", false);
},
/*
* Handle extension shut down
*/
shutdown: function() {
var os = CC['@mozilla.org/observer-service;1'].getService(CI.nsIObserverService);
os.removeObserver(this, "http-on-modify-request");
os.removeObserver(this, "http-on-examine-response");
os.removeObserver(this, "xpcom-shutdown");
},
/*
* Loads the required modules
*
* Note: if the module contains JavaScript errors, the resulting error will probably be "NS_ERROR_FILE_NOT_FOUND"!
*/
loadModules : function() {
var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var resProt = ioService.getProtocolHandler("resource").QueryInterface(Components.interfaces.nsIResProtocolHandler);
var extensionDir = __LOCATION__.parent.parent; //retrieves the extension directory
var modulesDir = extensionDir.clone();
modulesDir.append("modules");
var resourceURI = ioService.newFileURI(modulesDir);
resProt.setSubstitution("csfiremodules", resourceURI);
Components.utils.import("resource://csfiremodules/CsFireCommon.jsm");
CsFire.Logger.debug("Successfully loaded module dependencies");
},
/*
* Processes an outgoing HTTP request, which means that data is collected,
* a policy decision is made and enforced.
*/
processHttpRequest: function(subject) {
try {
subject.QueryInterface(Components.interfaces.nsIHttpChannel);
try {
CsFire.CsFireController.registerHttpRequest(subject);
CsFire.CsFireController.decideForURI(subject.URI, subject);
}
catch(e2) {
CsFire.Logger.error("Error processing request: " + e2);
subject.cancel(Components.results.NS_BINDING_ABORTED);
}
}
catch(e1) {
CsFire.Logger.error("Error processing request: not an HTTP channel? (" + e1 + ")");
subject.cancel(Components.results.NS_BINDING_ABORTED);
}
},
/*
* Processes an HTTP response, which has an influence on requests requiring
* HTTP authentication.
*/
processHttpResponse: function(subject) {
/* TODO Enable stripping of authentication headers
* Code is disabled due to bug 537670
* see https://bugzilla.mozilla.org/show_bug.cgi?id=537670
*/
/*
try {
subject.QueryInterface(Components.interfaces.nsIHttpChannel);
try {
CsFire.CsFireController.registerHttpResponse(subject);
}
catch(e2) {
CsFire.Logger.error("Error processing response: " + e2);
subject.cancel(Components.results.NS_BINDING_ABORTED);
}
}
catch(e1) {
CsFire.Logger.error("Error processing response: not an HTTP channel? (" + e1 + ")");
subject.cancel(Components.results.NS_BINDING_ABORTED);
}*/
},
/******************************
* nsIContentPolicy interface *
*****************************/
/*
* Implementation of the shouldLoad function. This function is called from the browser
* at the moment a new request is initiated (from the web page, address bar, ...). This
* means that no HTTP data is available at this point (too soon).
*/
shouldLoad: function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra) {
CsFire.CsFireController.registerLoadRequest(aContentLocation, aRequestOrigin, aContentType, aContext);
// Accept by default: no decision can be made at this point
return CICP.ACCEPT;
},
/*
* Implementation of the shouldProcess function. Called after the information about
* the resource has been determined for sure. Not needed here, since this point is
* too late to prevent CSRF (resource has probably been partly loaded).
*/
shouldProcess: function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeType, aExtra) {
return CICP.ACCEPT;
}
};
var components = [CsFireService];
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(components);
}